package com.wangxiaotao.springcloud.alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.wangxiaotao.springcloud.alibaba.handle.CustomerBlockHandle;
import com.wangxiaotao.springcloud.entities.CommonResult;
import com.wangxiaotao.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class RateLimitController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public CommonResult<Payment> byResource(){
        return new CommonResult<>(200, "按资源名称限流测试", new Payment(2020L, "serial001"));
    }


    public CommonResult<String> handleException(BlockException e){
        return new CommonResult<>(444, e.getClass().getCanonicalName() + ": 服务不可用");
    }

    @GetMapping("/rateLimit/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult<Payment> byUrl(){
        return new CommonResult<>(200, "按url限流测试", new Payment(2020L, "serial002"));
    }

    @GetMapping("/rateLimit/customerBlockHandle")
    @SentinelResource(value = "customerBlockHandle",
            blockHandlerClass = CustomerBlockHandle.class,
            blockHandler = "handleException1")
    public CommonResult<Payment> customerBlockHandle(){
        return new CommonResult<>(200, "客户自定义", new Payment(2020L, "serial003"));
    }

}
